package xyz.fuqufyb.aec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.fuqufyb.aec.dao.RoleMapper;
import xyz.fuqufyb.aec.data.JsonResult;
import xyz.fuqufyb.aec.entity.Role;
import xyz.fuqufyb.aec.service.IRoleService;

import java.util.List;
import java.util.Map;

/**
 * @author 用户: fyb
 * @description class: RoleServiceImpl
 * @date 时间: 2021/1/30 14:16
 */
@Service("IRoleService")
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> getRoleListUserId(Integer userId) {
        List<Role> roles = roleMapper.getRoleListUserId(userId);
        if (!roles.isEmpty()) {

            return roles;
        }
        return null;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {

        int i = roleMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return i;
        }
        return 0;
    }

    @Override
    public int insert(Role record) {
        int insert = roleMapper.insert(record);

        if (insert > 0) {
            return insert;
        }
        return 0;
    }

    @Override
    public int insertSelective(Role record) {

        int i = roleMapper.insertSelective(record);

        if (i > 0) {

            return i;
        }
        return 0;
    }

    @Override
    public Role selectByPrimaryKey(Integer id) {

        Role role = roleMapper.selectByPrimaryKey(id);

        if (role != null) {

            return role;
        }
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {

        int i = roleMapper.updateByPrimaryKeySelective(record);

        if (i > 0) {
            return i;
        }
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Role record) {

        int i = roleMapper.updateByPrimaryKey(record);

        if (i > 0) {
            return i;
        }
        return 0;
    }

    /**
     * @param maps
     * @return
     */
    @Override
    public JsonResult getRolePage(Map<String, Object> maps) {

        PageHelper.startPage(Integer.parseInt((String) maps.get("page")), Integer.parseInt((String) maps.get("limit")
        ));
        List<Role> roles = roleMapper.getRolePage(maps);

        if (!roles.isEmpty()) {
            PageInfo pageInfo = new PageInfo(roles);
            return JsonResult.getJsonSuccess(pageInfo);
        }
        return null;
    }

    /**
     * 处理用户权限数据
     *
     * @param userId
     * @param asList
     * @return
     */
    @Transactional
    @Override
    public Integer updateAssignRole(Integer userId, List<Integer> asList) {

        if (userId == 0 && asList.isEmpty()) {
            return 0;
        }
        /**
         * 先进行删除 成功
         */
        roleMapper.deleteUserRoleById(userId);
        /**
         * 在进行批量新增数据
         */
        Integer i = roleMapper.updateAssignRole(userId, asList);
        if (i > 0) {
            return i;
        }
        return 0;
    }

    @Override
    public List<Role> getRoleList() {
        List<Role> roles = roleMapper.getRoleList();

        if (!roles.isEmpty()) {
            return roles;
        }

        return null;
    }


}
